package cn.yunhe.controller;

import cn.yunhe.entity.Car;
import cn.yunhe.entity.Driver;
import cn.yunhe.service.ICarService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/after/UpdateCarServlet")
public class UpdateCarServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //编辑车辆信息
        Car searchCar = (Car) request.getSession().getAttribute("searchCar");
        String cardId = request.getParameter("cardId");
        String cname = request.getParameter("cname");
        int driverId = Integer.parseInt(request.getParameter("driverId"));
        int cvolume = Integer.parseInt(request.getParameter("cvolume"));
        Double cmileage = Double.parseDouble(request.getParameter("cmileage"));
        String ddate = request.getParameter("ddate");

        Driver driver = new Driver(driverId);
        Car car  = new Car(searchCar.getCarId(),cardId,cname,cvolume,cmileage,driver,ddate);

        //编辑车辆信息
        ICarService carService;
        carService = new ICarService();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if (carService.updateCar(car)){
            out.print(1);
        }else {
            out.print(0);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
